package drawing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// Homework 11 written by Mladen Yakimov (GTID: 902575580)
// Collaboration Statement:
// I did not discuss this assignment with anyone.

/**
* @author Mladen Yakimov
* This is HW11
* This is the MovableShapePanel class
* It extends the JPanel and is used to show the circles and rectangles
*/

public class MovableShapesPanel extends JPanel {

	private Timer timer;
	//initialize array with 3 rectangles and 3 circles
	private MovableShape[] movableShapeList ={ (new Rectangle(300, 300, 60, 60, 500, 500, 5, 5)),
	(new Rectangle(200, 200, 30, 30, 500, 500, 10, 20)),
	(new Rectangle(200, 200, 30, 30, 500, 500, 20, 10)),
	(new Rectangle(400, 400, 50, 50, 500, 500, 10, 20)),
	(new Circle(200, 200, 10, 100, 25)),
	(new Circle(200, 200, 20, 80, 10)),
	(new Circle(200, 200, 10, 100, 20)),
	(new Circle(300, 200, 10, 50, 5)),
	(new Circle(400, 400, 10, 20, 2))
	//(new Circle(50, 50, 50, 50, 50)),
	//(new Circle(100, 100, 10, 200, 180))
	};


	/**
	* This is the MovableShapesPanel constructor.
	*  it creates the timer sets the size and starts the timer
	*/
	public MovableShapesPanel() {
		timer = new Timer(20, new MovableShapeListener());
		setPreferredSize(new Dimension(500, 500));
		timer.start();

	}

	/**
	* This is overrdden paintComponent method. It paints the objects.
	* @param g the graphics object to paint the objects on.
	*/
	public void paintComponent (Graphics g) {

		super.paintComponent (g);

		for(int i=0; i<movableShapeList.length;i++) {
			MovableShape shape = movableShapeList[i];
			shape.draw(g);
		}
	}

	/**
	* @author Mladen Yakimov
	* This is HW11
	* This is the MovableShapeListener (Action Listener for the timer).
	* It implements the ActionListener
	*/
	private class MovableShapeListener implements ActionListener {

		/**
		* This is the actionPerfomed method.
		* @param ae the ActionEvent that triggered the listener.
		*/
		public void actionPerformed (ActionEvent ae) {

			for(int i=0; i<movableShapeList.length;i++) {
				MovableShape shape = movableShapeList[i];
				shape.move();
	        }

	        repaint();

		}
	}
}

